package com.burger.counter.servlets;

import java.io.IOException;
import java.util.Hashtable;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.burger.counter.beans.Item;
import com.burger.counter.beans.Kids;
import com.burger.counter.beans.ShoppingCart;
import com.burger.counter.beans.Starter;
import com.burger.counter.business.ItemBusiness;

/**
 * Servlet implementation class AddStarterToCart
 */
public class AddStarterToCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddStarterToCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			doPost(request, response);
		}
		catch(Exception e)
		{
			System.err.println(e);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			String[] idsSelected = request.getParameterValues("StarterData");
			if(idsSelected != null)
			{
				for (int i = 0; i < idsSelected.length; i++) {		 
					System.out.println(idsSelected[i] + "<br>");
					}
			}
			else
			{
				System.out.println("No starter Data Selected");
			}
			
			HttpSession session = request.getSession(true);
			Starter[] starterList = (Starter[]) session.getAttribute("StarterList");
			
			//preparing a chart
			ItemBusiness itemBI = new ItemBusiness();
			Item newItem = new Item();
			ShoppingCart currentCart = null;
			
			if(starterList != null && idsSelected != null)
			{
				for(int i =0 ;i<idsSelected.length;i++)
				{
					for(int j =0;j<starterList.length; j++)
					{
						if(Integer.parseInt(idsSelected[i]) == starterList[j].getStarterId())
						{
							newItem=  itemBI.setItemDetails(
									Integer.parseInt(idsSelected[i]),
									Integer.parseInt(session.getAttribute("UserId").toString()), 
									starterList[j].getStarterName(), 
									starterList[j].getStarterPrice());
						}
						
					}
				}
				
				currentCart=(ShoppingCart)session.getAttribute("Cart");
				
				if(currentCart == null)
				{
					System.out.println("Creating new cart....................");
					currentCart = new ShoppingCart();
					currentCart.addItemInCart(newItem);
					session.setAttribute("Cart", currentCart);
				}
				else
				{
					System.out.println("Cart is already there..................");
					currentCart.addItemInCart(newItem);
					session.setAttribute("Cart", currentCart);
				}
				
				session.setAttribute("StarterAdditionMessage", "Started item Added to Cart Successfully. ");
			}
			else
			{
				System.out.println("No Data Available");				
				session.setAttribute("SelectStarterMessage", "Please Select an Item to Add to Cart.");
			}
					
			
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/StarterMenu.jsp");
			dispatcher.forward(request, response);

		}
		catch(Exception e)
		{
			System.err.println(e);
		}
	}

}
